package Exam.String;

import java.util.Scanner;

public class StringD9 {
    //金额转换
    //例：2135 --> 零佰零拾零万贰仟壹佰叁拾伍元
    public static void main(String[] args) {
        int money;
        //1.录入过滤
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入金额");
        money = sc.nextInt();
        while (true) {
            if (money >= 0 && money <= 9999999) {
                break;
            } else {
                System.out.println("金额无效");
                break;
            }
        }

        //3.定义一个变量用来表示钱的大写
        String moneyStr = "";
        //得到每一位数字
        while (true){
            //获取个位
            int ge = money % 10;
            //将个位值传入方法找到对应的大写字符
            String capitalNumber = getCapitalNumber(ge);
            //把转换后的大写拼接到moneyStr当中
            moneyStr = capitalNumber + moneyStr;
            //去掉个位
            money = money / 10;
            //如果money为0 循环结束
            if (money == 0){
                break;
            }
        }

        //在前面补零
        int count = 7 - moneyStr.length();
        for (int i = 0; i < count; i++) {
            moneyStr = "零" + moneyStr;
        }

        //4.插入单位
        String[] arr = {"佰","拾","万","仟","佰","拾","元"};
        //遍历moneyStr,依次得到大写数字
        //然后把arr的单位插入进去
        String result = "";
        for (int i = 0; i < moneyStr.length(); i++) {
            char c =moneyStr.charAt(i);
            //把大写数字和单位拼接到result当中
            result = result + c + arr[i];
        }
        //5.打印
        System.out.print(result);
    }

    //2.定义一个方法把数字变成大写的中文
    public static String getCapitalNumber(int number){
        //定义数组，让数字跟大写的中文产生一个对应关系
        String[] arr = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
        //返回结果
        return arr[number];
    }
}
